package com.huawei.chengdu.meetingdemo.view.adapter;

import android.content.Context;
import android.text.SpannableStringBuilder;
import android.text.Spanned;
import android.text.TextUtils;
import android.text.style.ForegroundColorSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.huawei.chengdu.meetingdemo.R;
import com.huawei.chengdu.meetingdemo.model.AddrBookBean;
import com.huawei.chengdu.meetingdemo.util.LogUtil;
import com.huawei.chengdu.meetingdemo.view.dialogs.InviteSiteDialog;

import java.util.concurrent.CopyOnWriteArrayList;

public class InviteSiteAdapter extends RecyclerView.Adapter<InviteSiteAdapter.SiteViewHolder>{
    private static final String TAG = InviteSiteAdapter.class.getSimpleName();

    private Context mContext;

    private InviteSiteDialog inviteSiteDialog;

    private InviteSiteSelectedAdapter selectedAdapter;

    private CopyOnWriteArrayList<AddrBookBean> siteList = new CopyOnWriteArrayList<>();

    private CopyOnWriteArrayList<AddrBookBean> selectedSiteList = new CopyOnWriteArrayList<>();

    private String keyWord = "";

    public InviteSiteAdapter(Context mContext, InviteSiteDialog inviteSiteDialog) {
        this.mContext = mContext;
        this.inviteSiteDialog = inviteSiteDialog;
        this.selectedAdapter = inviteSiteDialog.getSelectedAdapter();
    }

    @NonNull
    @Override
    public SiteViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(mContext).inflate(R.layout.invite_site_item_layout, parent, false);
        return new SiteViewHolder(view);
    }
    @Override
    public void onBindViewHolder(@NonNull SiteViewHolder holder, int position) {
        holder.initData(position);
    }
    @Override
    public int getItemCount() {
        return siteList.size();
    }

    public void setKeyWord(String keyWord) {
        this.keyWord = keyWord;
    }

    /**
     * 填充数据
     *
     * @param sites 查询到的所有地址本
     */
    public void setData(CopyOnWriteArrayList<AddrBookBean> sites) {
        LogUtil.info(TAG + " setData() , size : " + sites.size());
        siteList.clear();
        siteList.addAll(sites);
        updateData();
    }

    /**
     * 刷新数据。
     * 1. 地址本搜索 ； 2，取消已勾选
     */
    public void updateData() {
        initSelectedStatus();
        this.notifyDataSetChanged();
    }

    private void initSelectedStatus() {
        if (selectedAdapter != null) {
            selectedSiteList = selectedAdapter.getSelectedSiteList();
        }
        if (selectedSiteList == null) {
            return;
        }

        for (AddrBookBean addrBookBean : siteList) {
            addrBookBean.setChecked(false);
        }

        for (int i = 0; i < selectedSiteList.size(); i++) {
            for (int j = 0; j < siteList.size(); j++) {
                if (selectedSiteList.get(i).equals(siteList.get(j))) {
                    siteList.get(j).setChecked(true);
                    break;
                }
            }
        }
    }

    /**
     * 与会者列表数据VH
     */
    class SiteViewHolder extends RecyclerView.ViewHolder {
        private LinearLayout llSiteItem;

        private ImageView ivSiteIcon;

        private TextView tvSiteName;

        private ImageView ivSelect;

        private int position;

        private boolean isSelected;

        private AddrBookBean curAddrBean;

        /**
         * 构造器
         *
         * @param itemView view
         */
        public SiteViewHolder(@NonNull View itemView) {
            super(itemView);
            initView(itemView);
        }

        private void initView(View itemView) {
            llSiteItem = itemView.findViewById(R.id.ll_site_item);
            ivSiteIcon = itemView.findViewById(R.id.iv_site_icon);
            tvSiteName = itemView.findViewById(R.id.tv_site_name);
            ivSelect = itemView.findViewById(R.id.iv_select);
        }

        /**
         * 初始化
         *
         * @param position pos
         */
        public void initData(int position) {
            LogUtil.info(TAG + " initData, pos=" + position);
            this.position = position;
            this.curAddrBean = siteList.get(position);
            isSelected = curAddrBean.isChecked();
            setIcon();
            setSiteName();
            setSelectedStatus();

            initEvent();
        }

        private void setIcon() {
            // 是否要根据图标区分本地和LDAP
            ivSiteIcon.setVisibility(View.VISIBLE);
            ivSiteIcon.setImageResource(curAddrBean.getInviteAvatarId());
        }

        private void setSiteName() {
            LogUtil.info(TAG + " setSiteName,pos=" + position);
            String siteName = curAddrBean.getName();
            if (!TextUtils.isEmpty(keyWord) && siteName.contains(keyWord)) {
                SpannableStringBuilder builder = new SpannableStringBuilder(siteName);
                int start = siteName.indexOf(keyWord);
                builder.setSpan(new ForegroundColorSpan(mContext.getColor(R.color.participant_dlg_chair_blue)), start,
                        start + keyWord.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
                tvSiteName.setText(builder);
            } else {
                tvSiteName.setText(siteName);
            }
        }

        private void setSelectedStatus() {
            ivSelect.setImageResource(isSelected ? R.drawable.invite_site_select : R.drawable.invite_site_unselect);
        }

        private void initEvent() {
            llSiteItem.setOnClickListener(view -> handleSelect());
            ivSelect.setOnClickListener(view -> handleSelect());
        }

        private void handleSelect() {
            LogUtil.info(TAG + " handleSelect");
            isSelected = !isSelected;
            curAddrBean.setChecked(isSelected);
            setSelectedStatus();
            updateSelectedList();
        }

        private void updateSelectedList() {
            if (selectedAdapter == null) {
                selectedAdapter = inviteSiteDialog.getSelectedAdapter();
                selectedSiteList = selectedAdapter.getSelectedSiteList();
            }
            if (isSelected) {
                LogUtil.info(TAG + " updateSelectedList,name=" + curAddrBean.getName());
                selectedSiteList.add(curAddrBean);
            } else {
                LogUtil.info(TAG + " updateSelectedList,remove Addrbean=" + curAddrBean.getName());
                selectedSiteList.remove(curAddrBean);
            }

            selectedAdapter.notifyDataSetChanged();
        }
    }
}
